//
//  GQHMobileController.m
//  Xanthium
//
//  Created by GuanQinghao on 02/03/2018.
//  Copyright © 2018 GuanQinghao. All rights reserved.
//

#pragma mark Macro
#import "GQHHeader.h"

#pragma mark Model
#import "GQHUserModel.h"

#pragma mark Object

#pragma mark View
#import "GQHMobileView.h"
#import "GQHPasswordView.h"

#pragma mark Controller
#import "GQHMobileController.h"
#import "GQHPasswordController.h"

#import "GQHRoleController.h"
#import "GQHTabBarController.h"

#pragma mark CocoaPods

#pragma mark ----------Header End----------

@interface GQHMobileController () <GQHMobileViewDelegate> {
    
#pragma mark Data
    GQHUserModel *userModel;
#pragma mark View
    /// 自定义根视图
    GQHMobileView *rootView;
}

@end


@implementation GQHMobileController

#pragma mark --Lifecycle
- (void)loadView {
    DLog();
    
    rootView = [[GQHMobileView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    rootView.qh_delegate = self;
    self.view = rootView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    DLog();
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    DLog();
    
    self.qh_titleLabel.text = @"验证手机号";
    [self.qh_rightButton setTitle:@"验证" forState:UIControlStateNormal];
    self.automaticallyAdjustsScrollViewInsets = NO;
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    DLog();
    
    if (!_qh_done) {
        
        rootView.qh_mobileTextField.userInteractionEnabled = NO;
        rootView.qh_mobileTextField.text = self.qh_mobile;
    }
}

#pragma mark --Delegate
// 请求验证码
- (void)qh_requestCodeWithMobile:(NSString *)mobile {
    DLog(@"%@",mobile);
    
    [GQHUserModel qh_requestCodeWithMobile:mobile success:^(BOOL status, NSNumber *code, NSString *message, GQHUserModel *user) {
        
        if (0 == [code intValue]) {
            
            userModel = user;
        }
    } failure:^(NSError *error) {
        
        NSString *errorString = [NSString stringWithFormat:@"网络出错:%@,code:%ld", error.domain, (long)error.code];
        [self qh_alertWithTitle:nil message:errorString actionTitle:nil handler:nil completion:nil];
    }];
}

#pragma mark --TargetMethod
- (void)qh_clickedRightButton:(UIButton *)sender {
    
    NSString *mobileString = rootView.qh_mobileTextField.text;
    NSString *codeString = rootView.qh_codeTextField.text;
    
    if ([mobileString isEqualToString:userModel.qh_mobile] && [codeString isEqualToString:userModel.qh_mobileCode]) {
        //    if ([mobileString isEqualToString:@"15102966484"] && [codeString isEqualToString:@"888888"]) {
        
        if ([self.qh_identifierType isEqualToString:@"binding"]) {
            
            [_qh_parameters setObject:mobileString forKey:@"phone"];
            
            NSString *URLString = [[NSString qh_APIURLString] stringByAppendingString:@"/loginRegist/1/chekThirdLogin"];
            
            [GQHNetHelper qh_requestGET:URLString parameters:_qh_parameters success:^(NSNumber *code, NSString *message, NSDictionary *data) {
                DLog(@"%@--%@--%@",code,message,data);
                
                if (0 == [code intValue]) {
                    
                    NSString *result = [data objectForKey:@"result"];
                    if ([result isEqualToString:@"1"]) {
                        
                        /// 绑定手机号
                        GQHMobileController *mobileController = [[GQHMobileController alloc] init];
                        mobileController.qh_identifierType = @"binding";
                        mobileController.qh_mobile = USER_GET_MOBILE;
                        mobileController.qh_done = YES;
                        mobileController.hidesBottomBarWhenPushed = YES;
                        [self.navigationController pushViewController:mobileController animated:YES];
                        
                    } else {
                        
                        GQHUserModel *userModel = [GQHUserModel yy_modelWithDictionary:data];
                        
                        [[NSUserDefaults standardUserDefaults] setObject:userModel.qh_userID forKey:USER_ID];
                        [[NSUserDefaults standardUserDefaults] setValue:userModel.qh_avatar forKey:USER_AVATAR];
                        [[NSUserDefaults standardUserDefaults] setObject:userModel.qh_userNickname forKey:USER_NAME];
                        [[NSUserDefaults standardUserDefaults] setObject:userModel.qh_mobile forKey:USER_MOBILE];
                        [[NSUserDefaults standardUserDefaults] setObject:userModel.qh_userTypeCode forKey:USER_TYPE];
                        [[NSUserDefaults standardUserDefaults] setObject:userModel.auditState forKey:USER_STATUS];
                        
                        NSNumber *score = [NSNumber numberWithFloat:userModel.qh_score];
                        [[NSUserDefaults standardUserDefaults] setValue:score forKey:USER_SCORE];
                        
                        [[NSUserDefaults standardUserDefaults] synchronize];
                        
                        if ([userModel.qh_userTypeCode isEqualToString:@"0"]) {
                            
                            [[NSUserDefaults standardUserDefaults] setValue:@"03" forKey:USER_STATUS];
                            
                            GQHRoleController *roleController = [[GQHRoleController alloc] init];
                            [self.navigationController pushViewController:roleController animated:YES];
                        } else {
                            
                            GQHTabBarController *tabBarController = [[GQHTabBarController alloc] init];
                            [[UIApplication sharedApplication] keyWindow].rootViewController = tabBarController;
                        }
                    }
                } else {
                    
                    [self showToastWithText:message];
                }
            } failure:^(NSError *error) {
                
                NSString *errorString = [NSString stringWithFormat:@"网络出错:%@,code:%ld", error.domain, (long)error.code];
                [self showToastWithText:errorString];
            }];
            
        } else {
            
            if ([self.qh_identifierType isEqualToString:@"password"]) {
                
                // 修改密码
                GQHPasswordController *passwordController = [[GQHPasswordController alloc] init];
                passwordController.qh_mobile = self.qh_mobile;
                [self.navigationController pushViewController:passwordController animated:YES];
            } else if ([self.qh_identifierType isEqualToString:@"mobile"] && !_qh_done) {
                
                // 修改手机号
                GQHMobileController *mobileController = [[GQHMobileController alloc] init];
                mobileController.qh_identifierType = self.qh_identifierType;
                mobileController.qh_done = YES;
                [self.navigationController pushViewController:mobileController animated:YES];
            } else {
                
                // 修改手机号接口，返回上两级页面
                NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
                [parameters setValue:mobileString forKey:@"phone"];
                [parameters setValue:USER_GET_ID forKey:@"userId"];
                
                [GQHUserModel qh_bindMobileWith:parameters success:^(BOOL status, NSNumber *code, NSString *message) {
                    
                    if (0 == [code integerValue]) {
                        
                        [[NSUserDefaults standardUserDefaults] setObject:mobileString forKey:USER_MOBILE];
                        
                        [self showToastWithText:message];
                        
                        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                            
                            // 返回上两级页面
                            NSUInteger index = [[self.navigationController viewControllers] indexOfObject:self];
                            UIViewController *controller = [[self.navigationController viewControllers] objectAtIndex:index - 2];
                            [self.navigationController popToViewController:controller animated:YES];
                        });
                    } else {
                        
                        [self showToastWithText:message];
                    }
                } failure:^(NSError *error) {
                    
                    NSString *errorString = [NSString stringWithFormat:@"网络出错:%@,code:%ld", error.domain, (long)error.code];
                    [self qh_alertWithTitle:nil message:errorString actionTitle:nil handler:nil completion:nil];
                }];
            }
        }
    } else {
        
        [self showToastWithText:@"您输入的手机号验证码不匹配,请重新输入！"];
    }
}
#pragma mark --PrivateMethod

#pragma mark --Setter

#pragma mark --Getter

#pragma mark --View

@end
